package com.leostadyn.stackfarm.timestatistics.model.mentity.admin;

import android.os.Handler;

import com.leostadyn.stackfarm.timestatistics.model.bean.DeleteBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.DetailProjectBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.EmployeeBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.StaffTimeRecordBean;
import com.leostadyn.stackfarm.timestatistics.model.minterface.admin.IFetchListModel;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.CompositeDisposable;

public class FetchListModel {
    public static void refresh(String end, String start, CompositeDisposable compositeDisposable, final IFetchListModel callback) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                AdminApi.getInstance().getEmployeeInformation().subscribe(new OriginalSubscriber<List<EmployeeBean>>(compositeDisposable) {

                    @Override
                    public void onError(ResponseThrowable e) {
                        callback.onFailure(e.getMsg());
                        callback.onCompleted();
                    }

                    @Override
                    public void onNext(List<EmployeeBean> employeeBeans, String msg) {
                        AdminApi.getInstance().getProjectInformation(end, start).subscribe(new OriginalSubscriber<List<DetailProjectBean>>(compositeDisposable) {

                            @Override
                            public void onError(ResponseThrowable e) {
                                callback.onFailure(e.getMsg());
                                callback.onCompleted();
                            }

                            @Override
                            public void onNext(List<DetailProjectBean> detailProjectBeans, String msg) {
                                callback.onSuccess((ArrayList) detailProjectBeans, (ArrayList) employeeBeans);
                                callback.onCompleted();
                            }
                        });

                    }

                });


            }
        }, 1000);
    }

    public static void toPersonnel(String end, String start, String projectId, CompositeDisposable compositeDisposable, IFetchListModel callback) {
        AdminApi.getInstance().getProjectDetail(end, start, projectId).subscribe(new OriginalSubscriber<List<EmployeeBean>>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                callback.onFailure(e.getMsg());
                callback.onCompleted();
            }

            @Override
            public void onNext(List<EmployeeBean> employeeBeans, String msg) {
                callback.onSuccessEmployee((ArrayList) employeeBeans);
                callback.onCompleted();
            }
        });

    }

    public static void toProject(String end, String start, String uid, CompositeDisposable compositeDisposable, IFetchListModel callback) {
        AdminApi.getInstance().getPersonnelDetails(end, start, uid).subscribe(new OriginalSubscriber<List<DetailProjectBean>>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                callback.onFailure(e.getMsg());
                callback.onCompleted();
            }

            @Override
            public void onNext(List<DetailProjectBean> detailProjectBeans, String msg) {
                callback.onSuccessProject((ArrayList) detailProjectBeans);
                callback.onCompleted();
            }
        });
    }

    public static void toTimeRecord(HashMap<String,String> map,CompositeDisposable compositeDisposable, IFetchListModel callback) {
        AdminApi.getInstance().getTimeRecord(map).subscribe(new OriginalSubscriber<StaffTimeRecordBean>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                callback.onFailure(e.getMsg());
                callback.onCompleted();
            }

            @Override
            public void onNext(StaffTimeRecordBean staffTimeRecordBean, String msg) {
                callback.onSuccessTimeRecord(staffTimeRecordBean);
                callback.onCompleted();
            }
        });
    }

    public static void toDeleteTimeRecord(HashMap<String, String> map, CompositeDisposable compositeDisposable, IFetchListModel callback) {
        AdminApi.getInstance().deleteTimeRecord(map).subscribe(new OriginalSubscriber<DeleteBean>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                callback.onFailure(e.getMsg());
                callback.onCompleted();
            }

            @Override
            public void onNext(DeleteBean deleteBean, String msg) {
                callback.onCompleted();
            }

        });
    }
}
